<?php

class ConferenceElementManager extends TModule
{
	// -------------------- basic functions --------------------
	public function addConferenceElement(	$name, $is_session, $start_datetime, $duration, $price,
											$quantity, $description, $owner_id, $place_id, $is_plan = false)
	{
		$element = new ConferenceElementRecord();
		$element->setData(	0, $name, $is_session, $start_datetime, $duration, $price,
							$quantity, $description, $owner_id, $place_id, $is_plan);
		
		if($element->save())
			return $element;
		else
			return null;
	}
	
	public function deleteConferenceElement($id)
	{
		return ConferenceElementRecord::finder()->deleteById($id);
	}
	
	public function updateConferenceElement($id, $name, $is_session, $start_datetime, $duration, $price,
											$quantity, $description, $owner_id, $place_id, $is_plan = false)
	{	
		$elementRecord = new ConferenceElementRecord();
		$elementRecord->setData($id, $name, $is_session, $start_datetime, $duration, $price,
								$quantity, $description, $owner_id, $place_id);
		return $this->updateConferenceElementByConferenceElementRecord($elementRecord);
	}

	public function updateConferenceElementByConferenceElementRecord($elementRecord)
	{	
		if($elementRecord instanceof ConferenceElementRecord)
		{
			$finder = ConferenceElementRecord::finder();
			$id = $elementRecord->getId();
			if($id > 0 && $rec = $finder->findByPk($id))
			{
				if($rec->equals($elementRecord))
					return $rec;
				$rec->setDataByConferenceElementRecord($elementRecord);
				if($rec->save())
					return $rec;
			}
		}
		return null;
	}
	
	public function getConferenceElementById($id)
	{
		return ConferenceElementRecord::finder()->findById($id);
	}
	
	// -------------------- extended functions --------------------
	public function getConferenceElementsList($revert = false, $offset = -1, $limit = -1)
	{
		$criteria = new TActiveRecordCriteria();
		
		if($revert)
			;//$criteria->OrdersBy['start_datetime'] = 'desc';
			
		if($offset < 0 || $offset == null)
			$offset = 0;
		if($limit < 0 || $limit == null)
			$limit = $this->countConferenceElements();
			
		$criteria->Offset = $offset;
		$criteria->Limit = $limit;
		
		return ConferenceElementRecord::finder()->with_place()->findAll($criteria);
	}
	
	public function getConferenceElementsListByOwnerId($owner_id, $revert = false, $offset = -1, $limit = -1)
	{
		$criteria = new TActiveRecordCriteria();
		$criteria->Condition = 'owner_id = :owner_id';
		$criteria->Parameters[':owner_id'] = $owner_id;
		
		if($revert)
			;//$criteria->OrdersBy['start_datetime'] = 'desc';
			
		if($offset < 0 || $offset == null)
			$offset = 0;
		if($limit < 0 || $limit == null)
			$limit = $this->countConferenceElementsByOwnerId($owner_id);
			
		$criteria->Offset = $offset;
		$criteria->Limit = $limit;
		
		return ConferenceElementRecord::finder()->with_place()->findAll($criteria);
	}
	
	public function getConferenceElementsListByIsSession($is_session = true, $revert = false, $offset = -1, $limit = -1)
	{
		$criteria = new TActiveRecordCriteria();
		$criteria->Condition = 'is_session = :is_session';
		$criteria->Parameters[':is_session'] = $is_session ? '1' : '0';
		
		if($revert)
			;//$criteria->OrdersBy['start_datetime'] = 'desc';
			
		if($offset < 0 || $offset == null)
			$offset = 0;
		if($limit < 0 || $limit == null)
			$limit = $this->countConferenceElementsByIsSession($is_session);
			
		$criteria->Offset = $offset;
		$criteria->Limit = $limit;
		return ConferenceElementRecord::finder()->with_place()->findAll($criteria);
	}
	
	public function getConferenceElementsListByIsPlan($is_plan = true, $revert = false, $offset = -1, $limit = -1)
	{
		$criteria = new TActiveRecordCriteria();
		$criteria->Condition = 'is_plan = :is_plan';
		$criteria->Parameters[':is_plan'] = $is_plan ? '1' : '0';
		
		if($revert)
			;//$criteria->OrdersBy['start_datetime'] = 'desc';
			
		if($offset < 0 || $offset == null)
			$offset = 0;
		if($limit < 0 || $limit == null)
			$limit = $this->countConferenceElementsByIsPlan($is_plan);
			
		$criteria->Offset = $offset;
		$criteria->Limit = $limit;
		return ConferenceElementRecord::finder()->with_place()->findAll($criteria);
	}
	
	public function countConferenceElements()
	{
		return ConferenceElementRecord::finder()->count();
	}

	public function countConferenceElementsByOwnerId($owner_id)
	{
		$criteria = new TActiveRecordCriteria();
		$criteria->Condition = 'owner_id = :owner_id';
		$criteria->Parameters[':owner_id'] = $owner_id;
		
		return ConferenceElementRecord::finder()->count($criteria);
	}
	
	public function countConferenceElementsByIsSession($is_session)
	{
		$criteria = new TActiveRecordCriteria();
		$criteria->Condition = 'is_session = :is_session';
		$criteria->Parameters[':is_session'] = $is_session ? '1' : '0';
		
		return ConferenceElementRecord::finder()->count($criteria);
	}
	
	public function countConferenceElementsByIsPlan($is_plan)
	{
		$criteria = new TActiveRecordCriteria();
		$criteria->Condition = 'is_plan = :is_plan';
		$criteria->Parameters[':is_plan'] = $is_plan ? '1' : '0';
		
		return ConferenceElementRecord::finder()->count($criteria);
	}
}

?>